<?php
namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\Hash;
use function Matrix\add;    //引入hash

class MallOrdersRepository{

    /**
     * 新增产品订单
     * @param $param
     * @return bool
     *@throws BasicException
     */
    public function  addMallOrder($param){
        $add = app('model_mall_orders')->create($param);
        if ($add)  return  $add;
        throw new BasicException(93111);
    }

    /**
     * 获取产品订单列表
     * @param $where
     * @param $page
     * @param $perPage
     * @return bool
     *@throws BasicException
     */
    public  function  MallOrderList($where,$page=1,$perPage=20){
        $list = app('model_mall_orders')
                ->with('user_buyer:user_id,nickname')
                ->with('user_owner:user_id,nickname')
                ->with('mall_products:id,title,picture')
                ->with('mall_categories:id,title')
                ->orderBy('id','desc');
        if (!empty($where['where_order'])){
            $list =$list->where($where['where_order']);
        }

        if (!empty($where['where_buyer'])){
            $list =$list->whereHas('user_buyer',function ($query) use($where){
                $query->where($where['where_buyer']);
            });
        }

        if (!empty($where['where_owner'])){
            $list =$list->whereHas('user_owner',function ($query) use($where){
                $query->where($where['where_owner']);
            });
        }

        if (!empty($where['where_product'])){
            $list =$list->whereHas('mall_products',function ($query) use($where){
                $query->where($where['where_product']);
            });
        }
        $list=$list->paginate($perPage, ['*'], '', $page);

        return $list;
    }

    // 赠送
    public function give($product_id, $user_id, $use_days = 0) {
        if ($product_id <= 0) {
            throw new BasicException(10001, "请选择商品!");
        }
        if ($user_id <= 0) {
            throw new BasicException(10001, "请选择用户!");
        }
        $product = app("model_mall_products")->where("id", $product_id)->first();
        if (!$product) {
            throw new BasicException(10001, "商品不存在!");
        }
        $price = app("model_mall_products_price")->where([
            ['product_id','=', $product_id]
        ])->first();
        if (!$price) {
//            throw new BasicException(10001, "定价不存在!");
        }

        $user = app("repo_user")->getUserInfo($user_id);
        if (!$user) {
            throw new BasicException(10001, "用户不存在!");
        }

        $detail = app("model_mall_orders")->where([
            ['product_id' , "=", $product_id],
            ['user_id_owner' , "=", $user_id]
        ])->first();
        if ($detail) {
            if ($detail['state'] == 2 || $detail['expired_time'] < time()) {
                $update_data = [
                    'state'        => 0,
                    'expired_time' => time() + $use_days * 86400
                ];
            } else {
                $update_data = [
                    'expired_time' => $detail['expired_time'] + $use_days * 86400
                ];
            }
            $re = app("model_mall_orders")->where([['id' ,"=", $detail['id']]])->update($update_data);
        } else {
            $expired_time = -1;
            if ($use_days > 0) {
                $expired_time = time() + $use_days * 86400;
            }
            $re = app("model_mall_orders")->create([
                'user_id_buyer' => 0,
                'user_id_owner' => $user_id,
                'product_id'    => $product_id,
                'price_id'      => $price['id'] ?? 0,
                'state'         => 0,
                'add_time'      => time(),
                'price'         => floatval($price['price'] ?? 0),
                'day'           => intval($price['day'] ?? 0),
                'type'          => 3,
                'expired_time'  => $expired_time,
                'category_id'   => $product['category_id'],
                'coin'          => 0
            ]);
        }
        if (!$re) {
            throw new BasicException(10001, "赠送失败，请重试!");
        }

        return true;
    }

}
